package com.pai.servlet;



import com.alibaba.fastjson.JSON;
import com.pai.entity.AppRole;
import com.pai.service.Role.RoleService;
import com.pai.service.Role.RoleServiceImpl;
import com.pai.vo.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author amu_1115@126.com
 * @version 1.0
 */
@WebServlet(name = "roleServlet",urlPatterns = {"/role"})
public class RoleServlet extends BaseServlet{
    private RoleService roleService ;  //服务层

    @Override
    public void init() throws ServletException {
        super.init();
        this.roleService = new RoleServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();

        try{
            String action =req.getParameter("action");
            switch (action){
                case "list":
                    List<AppRole> list =roleService.getList();
                    apiResult.setData(list);
                    break;
                case "detail":
                    String id = req.getParameter("id");
                    AppRole itemDetail = roleService.getItemDetail(id);
                    apiResult.setData(itemDetail);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }

    //修改
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        AppRole role = JSON.parseObject(jsonString, AppRole.class);

        flag = roleService.update(role);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("更新失败");
        }

        super.write(resp, apiResult);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String id = req.getParameter("id");

        flag = roleService.delete(id);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("删除失败");
        }

        super.write(resp, apiResult);
    }

    //新增
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        AppRole role = JSON.parseObject(jsonString, AppRole.class);

        try{
            flag = roleService.add(role);
       }catch (Exception e){
           e.printStackTrace();
           apiResult.setError();
       }finally {
            write(resp, apiResult);
        }
    }
}
